angular.module('starter.controllers', [])

  .controller('AppCtrl', function ($scope, $ionicModal, $timeout ,$cordovaCamera) {


    // With the new view caching in Ionic, Controllers are only called
    // when they are recreated or on app start, instead of every page change.
    // To listen for when this page is active (for example, to refresh data),
    // listen for the $ionicView.enter event:
    //$scope.$on('$ionicView.enter', function(e) {
    //});




    // Form data for the login modal
    $scope.loginData = {};

    // Create the login modal that we will use later
    $ionicModal.fromTemplateUrl('templates/login.html', {
      scope: $scope
    }).then(function (modal) {
      $scope.modal = modal;
    });

    // Triggered in the login modal to close it
    $scope.closeLogin = function () {
      $scope.modal.hide();
    };

    // Open the login modal
    $scope.login = function () {
      $scope.modal.show();
    };

    $scope.$watch('loginData.username', function (newVal, oldVal) {
      console.log(newVal);
      if(newVal!=null&&newVal!=""){
        if (newVal.length > 10)
          alert("登录名不对");
    }
    });

    // Perform the login action when the user submits the login form
    $scope.doLogin = function () {
      console.log('Doing login', $scope.loginData);

      // Simulate a login delay. Remove this and replace with your login
      // code if using a login system
      $timeout(function () {
        $scope.closeLogin();
      }, 1000);
    };



    //use Camera
    $scope.doGetPicture=function(){
      document.addEventListener("deviceready", function () {

        var options = {
          quality: 50,
          destinationType: Camera.DestinationType.DATA_URL,
          sourceType: Camera.PictureSourceType.CAMERA,
          allowEdit: true,
          encodingType: Camera.EncodingType.JPEG,
          targetWidth: 100,
          targetHeight: 100,
          popoverOptions: CameraPopoverOptions,
          saveToPhotoAlbum: false,
          correctOrientation:true
        };

        $cordovaCamera.getPicture(options).then(function(imageData) {
          var image = document.getElementById('myImage');
          image.src = "data:image/jpeg;base64," + imageData;
        }, function(err) {
          // error
        });

      }, false);
    };

    //test 自定义Plug
    $scope.doPlugin=function(){
      var param = {"content": "这是测试"};
      ExtraInfo.getExtra(param,function(success){
        //alert(success);
        console.log(success);
      },function(error){
        //alert(error);
        console.log(error);
      });
    };


  })

  .controller('PlaylistsCtrl', function ($scope) {
    $scope.playlists = [
      {title: 'Reggae', id: 1, class: 'icon ion-ios-telephone'},
      {title: 'Chill', id: 2, class: 'icon ion-ios-telephone'},
      {title: 'Dubstep', id: 3, class: 'icon ion-ios-telephone'},
      {title: 'Indie', id: 4, class: 'icon ion-ios-telephone'},
      {title: 'Rap', id: 5, class: 'icon ion-ios-telephone'},
      {title: 'Cowbell', id: 6, class: 'icon ion-ios-telephone'}
    ];
  })

  .controller('PlaylistCtrl', function ($scope, $q, $http, $ionicPopup, $stateParams) {
    $scope.hasmore = true;
    var ym = 0;//页面计数

    ///* state:1初始化，2刷新，3加载更多 */
    jzym(1);
    console.log("进入刚进来");

    //下拉刷新
    $scope.doRefresh = function () {
      console.log("进入doRefresh");
      jzym(2);
      $scope.$broadcast('scroll.refreshComplete');
    };

    //上拉
    $scope.loadMore = function () {
      console.log("进入LOADMORE");
      console.log($scope.customers);
      jzym(3);
      console.log(ym);
      $scope.$broadcast('scroll.infiniteScrollComplete');

    };

    ///* state:1初始化，2刷新，3加载更多 */
    function jzym(state) {
      var param = {};
      if (state == 1) {
        param.url = "templates/data1.php";
        getData(param).then(function (result) {
          console.log(result);
          $scope.customers = result.records;
        });
      }
      else if (state == 2) {
        param.url = "templates/Customers_JSON.php";
        getData(param).then(function (result) {
          console.log(result);
          $scope.customers = result.records;
        });
      }
      else if (state == 3) {
        param.url = "templates/Customers_JSON.php";
        getData(param).then(function (result) {
          $scope.customers = $scope.customers.concat(result.records);
          //$scope.customers = result.records;
          if (result.records == null || result.records == 0) {
            console.log("结束");
            $scope.hasmore = false;
          } else {
            ym = ym + 1;
          }
        });
      }
    };

    $scope.cancle = function (item) {
      console.log("返销了" + item.Name);
    };

    function getData(param) {
      var deferred = $q.defer();
      console.log(param.url);
      $http.get(param.url)
        .success(function (data) {
          deferred.resolve(data);
        })
      return deferred.promise;
    };


  });
